package com.example.yy.startproject_223yy.model;

import com.example.yy.startproject_223yy.bean.WeatherResult;
import com.example.yy.startproject_223yy.iface.WeatherListener;
import com.example.yy.startproject_223yy.iface.Weatheriface;
import com.example.yy.startproject_223yy.service.Weather51Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yy on 2017/3/28.
 */

public class Weather51Model implements Weatheriface {
    private Retrofit retrofit;
    private  String BASEURL="http://www.weather.51wnl.com/weatherinfo/";
    private Weather51Service weather51Service;

    public Weather51Model(){
        //Retrofit使用3-1
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }


    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
        //Retrofit使用3-2
        weather51Service=retrofit.create(Weather51Service.class);
        Call<WeatherResult> call =weather51Service.getRsult(cityno,1);
        //Retrofit使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onRsponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {

            }
        });


    }
}